package com.sxg.controller;

import com.sxg.query.SxgAddressQuery;
import com.sxg.utils.code.AjaxResult;
import com.sxg.vo.SxgAddressVO;
import com.sxg.vo.req.AddressDefaultReqVo;
import com.sxg.vo.req.AddressRemoveReqVo;
import com.sxg.vo.req.AddressSaveReqVo;
import lombok.AllArgsConstructor;
import com.sxg.page.PageResult;
import com.sxg.utils.code.Result;
import com.sxg.service.SxgAddressService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;

import java.util.List;

/**
 * 地址表
 *
 * @author 浪漫写尽歌谱 3298751420@qq.com
 * @since 1.0.0 2024-03-07
 */
@RestController
@RequestMapping("sxg/sxg_address")
@AllArgsConstructor
public class SxgAddressController {
    private final SxgAddressService sxgAddressService;

    @GetMapping("page")
    @PreAuthorize("hasAuthority('sxg:sxg_address:page')")
    public Result<PageResult<SxgAddressVO>> page(@Valid SxgAddressQuery query) {
        PageResult<SxgAddressVO> page = sxgAddressService.page(query);

        return Result.ok(page);
    }

    @GetMapping("{id}")
    public AjaxResult get(@PathVariable("id") Long id) {
        return sxgAddressService.selectById(id);
    }

    @PostMapping
    public AjaxResult save(@RequestBody @javax.validation.Valid AddressSaveReqVo vo) {
        sxgAddressService.save(vo);
        return AjaxResult.success();
    }

    @GetMapping("list")
    public AjaxResult list() {
        return sxgAddressService.selectByIdList();
    }

    @PostMapping("default")
    public AjaxResult setDefaultAddress(@RequestBody @Valid AddressDefaultReqVo vo) {
        return sxgAddressService.setDefaultAddress(vo.getId());
    }

    @GetMapping("default")
    public AjaxResult getDefaultAddress() {
        return sxgAddressService.getDefaultAddress();
    }

    @PutMapping
    public AjaxResult update(@RequestBody @Valid SxgAddressVO vo) {
        return sxgAddressService.update(vo);
    }

    @DeleteMapping
    public AjaxResult delete(@RequestBody AddressRemoveReqVo vo) {
        return sxgAddressService.delete(vo.getId());
    }
}